package com.gexin.model.product;

import com.gexin.model.product.base.BaseProductSkuValue;
import com.gexin.model.product.base.SkuJsonList;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务名称:  t_product_sku_value(商品SKU 属性表)
 */
@SuppressWarnings("serial")
public class ProductSkuValue extends BaseProductSkuValue<ProductSkuValue> {
	public static final ProductSkuValue dao = new ProductSkuValue();
	/**
	 * fang 2016-06-15
	 * 根据key_id  找到对应的 value
	 *
	 */
	public List<ProductSkuValue>  getValueByKeyId(int key_id){
		return dao.find("SELECT value_id,product_id,key_id,value_name  from t_product_sku_value where deleted=0 and key_id=?",key_id);
	}
	/**
	 * 根据一个商品ID 查询  他的SKU ID 并且转换JSON
	 *   肖得明
	 * @param product_id
	 * @return [{"name":"尺码","sku":{"156":"XXL","155":"XL","158":"S","157":"XXXL"}},{"name":"颜色","sku":{"154":"绿色","153":"红色","152":"黑色"}}]
	 */
	public String getSkuValueByProductId(Integer product_id){
		String sql ="select  k.key_name ,group_concat(v.value_name) as sku_key,group_concat(v.value_id) as sku_id from t_product_sku_value  as v , t_product_sku_key as k where  v.deleted=0 and v.product_id=? and k.key_id=v.key_id  GROUP BY (k.key_name)";
		List<Record> list_record =Db.find(sql,product_id);
		List<SkuJsonList> json_list=new ArrayList<SkuJsonList>();
		//解析JSON
		if(list_record!=null  && list_record.size()>0){
			for(Record item_record:list_record){
				SkuJsonList item_json= new SkuJsonList();
				item_json.setName(item_record.getStr("key_name"));
				String sp_name[] = item_record.getStr("sku_key").split(",");
				String sp_id[] = item_record.getStr("sku_id").split(",");
				for(int i=0;i<sp_name.length;i++){
					item_json.getSku().put(sp_id[i], sp_name[i]);
				}
				json_list.add(item_json);
			}
		}
		return JsonKit.toJson(json_list);
	}

	/**
	 * 添加商品的时候    根据SKU ID 查询SKU TEXT
	 * @param product_id  商品ID
	 * @param sku_id   列:111_23222_344
	 * @return  颜色：黑色,尺码：XL
	 */
	public String getSkuTextBySkuId(int product_id,String  sku_id){
		String sql ="select group_concat(k.key_name,'：',v.value_name) as sku_text from t_product_sku_value as v , t_product_sku_key as k where v.product_id=? and v.key_id=k.key_id and v.value_id in("+sku_id.replaceAll("_", ",")+")";
		return Db.queryStr(sql, product_id);
	}


	/**
	 * 方俊逸     2016-06-20
	 * 商品修改中   根据product_id  查到所有的key_id
	 *
	 */
	public List<ProductSkuValue> getKeyIdByProductId(String product_id){
		String sql="  select  distinct(key_id)  from t_product_sku_value where product_id =?  ";
		return dao.find(sql,product_id);
	}


	public List<ProductSkuValue> getKeyByProductId(String product_id){
		String sql="  select  *  from t_product_sku_value where product_id =?  ";
		return dao.find(sql,product_id);
	}
	/**
	 * 方俊逸   2016-06-21
	 *  商品修改中  根据  product_id 和key_id  得到对应的value
	 *
	 */
	public List<ProductSkuValue> getKeyIdByProductIdAndKeyId(String product_id, int key_id){
		String sql="  select  *  from t_product_sku_value where deleted=0 and product_id =? and key_id=? ";
		return dao.find(sql,product_id,key_id);
	}

	/**
	 * fang  2016-06-21
	 * 根据product  将  全部制为1
	 */
	public void setDeleteByProductId(int product_id){
		String sql=" update t_product_sku_value set deleted=1 where product_id=? ";
		Db.update(sql,product_id);
	}

	/**
	 * fang 2016-17
	 * 根据product_id 拿到所有的
	 */
	public List<ProductSkuValue> getValueListByProductId(int product_id){
		String sql=" select *  from t_product_sku_value  where product_id=?   ";
		return dao.find(sql,product_id);
	}
}
